package inet.app.vod.activity.common;

import inet.app.vod.activity.R;
import inet.app.vod.adapter.MenuAdapter;
import inet.app.vod.object.MenuUIItem;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

/**
 * Sliding menu fragment
 * 
 */
public class MenuListFragment extends ListFragment {

	public static int positionSelect = 0;

	private MenuAdapter mMenuAdapter;
	private List<MenuUIItem> mMenuItems = new ArrayList<MenuUIItem>();
	private static MenuListFragment mInstance;

	public static MenuListFragment newInstance(List<MenuUIItem> menuItems) {
		mInstance = new MenuListFragment();
		mInstance.setMenuItems(menuItems);
		return mInstance;
	}

	public void setMenuItems(List<MenuUIItem> menus) {
		mMenuItems = new ArrayList<MenuUIItem>(menus);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mMenuAdapter = new MenuAdapter(getActivity(), 0, mMenuItems, -1);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.menu_list, null);
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setListAdapter(mMenuAdapter);
	}

	public void notifyDataSetChanged(List<MenuUIItem> menuUIItems) {
		mMenuItems = new ArrayList<MenuUIItem>(menuUIItems);
		if (mMenuAdapter != null) {
			mMenuAdapter.notifyDataSetChanged();
		} else {
			mMenuAdapter = new MenuAdapter(getActivity(), 0, mMenuItems, -1);
		}
	}

	@Override
	public void onListItemClick(ListView lv, View v, int position, long id) {
		MenuUIItem menuUIItem = mMenuItems.get(position);
		menuUIItem.getListener().onClick();
		for (MenuUIItem item : mMenuItems) {
			item.setSelect(false);
		}
		mMenuItems.get(position).setSelect(true);
		positionSelect = position;
		mMenuAdapter.notifyDataSetChanged();
	}
}
